package com.example.springstudy.controller;

import com.example.springstudy.common.PagedResultDto;
import com.example.springstudy.common.SuccessResult;
import com.example.springstudy.dto.DataEventInput;
import com.example.springstudy.entity.DiseaseDetail;
import com.example.springstudy.services.DiseaseDetailRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

// 数据报警控制器
@RestController
@RequestMapping("api/diseaseDetail")
public class DiseaseDetailController {
    @Resource
    private DiseaseDetailRepository diseaseDetailRepository;



    @GetMapping("{id}")
    public SuccessResult<DiseaseDetail> getOne(@PathVariable long id) {
        Optional<DiseaseDetail> byId = diseaseDetailRepository.findById(id);
        if (byId.isPresent()) {
            return new SuccessResult<>(byId.get());
        }
        throw new RuntimeException(" 病 害 详 情  not found");
    }

    @GetMapping("")
    public SuccessResult<PagedResultDto<DiseaseDetail>> getList(DataEventInput input) {
        List<DiseaseDetail> list = (List<DiseaseDetail>) diseaseDetailRepository.findAll();
        PagedResultDto<DiseaseDetail> pageRes = new PagedResultDto<>();
        pageRes.setItems(list);
        pageRes.setTotal((long) list.size());
        return new SuccessResult<>(pageRes);
    }

}
